package com.ddxcloud.movie.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddxcloud.common.constants.PayMethodConstant;
import com.ddxcloud.common.constants.PayTypeConstant;
import com.ddxcloud.common.constants.enums.RefundStatusEnum;
import com.ddxcloud.common.handler.BusinessException;
import com.ddxcloud.common.utils.IpUtil;
import com.ddxcloud.common.utils.PayOrderUtil;
import com.ddxcloud.movie.config.common.CommonProperties;
import com.ddxcloud.movie.data.dto.RefundDTO;
import com.ddxcloud.movie.data.entity.PaymentOrder;
import com.ddxcloud.movie.data.entity.RefundOrder;
import com.ddxcloud.movie.mapper.RefundOrderMapper;
import com.ddxcloud.movie.service.RefundOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

@Service
@Slf4j
public class RefundOrderServiceImpl extends ServiceImpl<RefundOrderMapper, RefundOrder> implements RefundOrderService {

    @Resource
    CommonProperties commonProperties;

    @Override
    public RefundOrder createRefundOrder(RefundDTO dto, PaymentOrder paymentOrder, HttpServletRequest request) {
        String outRefundNo = dto.getOrderId() + "_" + RandomUtil.randomNumbers(4);
        RefundOrder refundOrder = new RefundOrder();
        refundOrder.setOrderSn(PayOrderUtil.getOrderSn(PayTypeConstant.Refund));
        refundOrder.setOrderId(paymentOrder.getOrderId());
        refundOrder.setProductName(paymentOrder.getProductName());
        refundOrder.setPayMethod(paymentOrder.getPayMethod());
        refundOrder.setRefundAmount(ObjectUtil.isEmpty(dto.getRefundAmount()) ? paymentOrder.getPayAmount() : dto.getRefundAmount());
        refundOrder.setRefundStatus(RefundStatusEnum.STATUS_0.getCode());
        if (Objects.equals(refundOrder.getPayMethod(), PayMethodConstant.WECHAT)) {
            refundOrder.setCallbackUrl(commonProperties.getDomainName() + "/refund/wechat/notify");
        }
        refundOrder.setClientIp(IpUtil.getClientIp(request));
        refundOrder.setReason(dto.getReason() != null ? dto.getReason() : "");
        refundOrder.setOutRefundNo(outRefundNo);
        log.info("创建退款订单-请求参数 {}", JSONUtil.toJsonStr(refundOrder));
        if (this.save(refundOrder)) {
            return refundOrder;
        }
        throw new BusinessException("创建退款订单失败，请联系管理员");
    }

    @Override
    public Boolean updateRefundOrderForRefund(RefundOrder refundOrder, Integer refundStatus) {
        LambdaUpdateWrapper<RefundOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(RefundOrder::getRefundStatus, refundStatus);
        updateWrapper.set(RefundOrder::getRefundTime, new Date());
        updateWrapper.eq(RefundOrder::getId, refundOrder.getId());
        return this.update(refundOrder, updateWrapper);
    }
}
